package com.atguigu.user.service.impl;

<<<<<<< HEAD
import com.atguigu.param.AddressParam;
=======
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
import com.atguigu.pojo.Address;
import com.atguigu.user.mapper.AddressMapper;
import com.atguigu.user.service.AddressService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

<<<<<<< HEAD
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
=======
import java.util.List;

/**
 * projectName: b2c-store
 * <p>
 * description: 地址业务实现类
 */
@Service
@Slf4j
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
<<<<<<< HEAD
     * 查询地址列表
     * @param userId
     * @return
=======
     * 根据用户id查询 地址数据!
     *   1.直接进行数据库查询
     *   2.结果封装即可
     * @param userId 用户id 已经校验完毕
     * @return 001 004
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
     */
    @Override
    public R list(Integer userId) {

<<<<<<< HEAD
        //1.参数处理
        if (userId == null){
            //此处,抛出异常,后续统一异常收集处理!
            log.info("AddressServiceImpl.list业务开始，参数:{}",userId);
            return R.fail("展示业务失败!");
        }
        //2.数据库查询
        QueryWrapper<Address> queryWrapper
                = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Address> addressList = addressMapper.selectList(queryWrapper);
        //3.结果处理
        R ok = R.ok(addressList);

        log.info("AddressServiceImpl.list业务结束，结果:{}",ok);

=======
        //1,封装查询参数
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);


        List<Address> addressList = addressMapper.selectList(queryWrapper);

        //2.结果封装
        R ok = R.ok("查询成功", addressList);
        log.info("AddressServiceImpl.list业务结束，结果:{}",ok);
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
        return ok;
    }

    /**
<<<<<<< HEAD
     * 保存数据库数据
     *
     * @param addressParam
     * @return
     */
    @Override
    public R save(AddressParam addressParam) {


        Address address = addressParam.getAdd();
        address.setUserId(addressParam.getUserId());

        //1.数据库插入
        int rows = addressMapper.insert(address);

        //2.返回结果处理
        if (rows == 0){
            return R.fail("地址保存失败!");
        }

        log.info("AddressServiceImpl.save业务结束，结果:{}",address);
        //调用查询,返回全部数据!
=======
     * 插入地址数据,插入成功以后,要返回新的数据集合!
     *
     * @param address 地址数据已经校验完毕哦!
     * @return 数据集合
     */
    @Override
    public R save(Address address) {

        //1.插入数据
        int rows = addressMapper.insert(address);
        //2.插入成功
        if (rows == 0){
            log.info("AddressServiceImpl.save业务结束，结果:{}","地址失败!");
            return R.fail("插入地址失败!");
        }
        //复用查询业务
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
        return list(address.getUserId());
    }

    /**
<<<<<<< HEAD
     * 删除地址数据
     *
     * @param id
     * @return
=======
     *
     * TODO:
     *   1.定义接收参数的param 并且添加参数校验注解
     *   2.定义controller
     *   3.定义service
     *   4.定义mapper
     *
     * 根据id 删除地址数据
     *
     * @param id 地址id
     * @return 结果 001  004
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
     */
    @Override
    public R remove(Integer id) {

<<<<<<< HEAD
        if (id == null){
           log.info("AddressServiceImpl.remove业务开始，参数:{}",id);
           return R.fail("地址移除失败!");
        }

        int rows = addressMapper.deleteById(id);


        log.info("AddressServiceImpl.remove业务结束，移除结果:{}",rows);

        if (rows == 0){

            return R.fail("地址移除失败!");
        }

        return R.ok("地址移除成功!");
=======
        int rows = addressMapper.deleteById(id);

        if (rows == 0){
            log.info("AddressServiceImpl.remove业务结束，结果:{}","地址删除失败");
            return R.fail("删除地址数据失败!");
        }

        log.info("AddressServiceImpl.remove业务结束，结果:{}","地址删除成功!");

        return R.ok("地址删除成功!");
>>>>>>> 6a60feb746291cdad2d7e97b2fe9933c5c00923d
    }
}
